import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Status } from 'src/dictionaries/index.enum';
import { GetNoticeDto } from 'src/modules/admin/notice/dto/notice.dto';
import { NoticeEntity } from 'src/modules/admin/notice/entities/notice.entity';
import { throwHttp } from 'src/utlis';
import { LessThanOrEqual, MoreThanOrEqual, Repository } from 'typeorm';

@Injectable()
export class NoticeService {
  constructor(
    @InjectRepository(NoticeEntity)
    private noticeEntityRepository: Repository<NoticeEntity>,
  ) { }
  async getNoticeList(params: GetNoticeDto) {
    try {
      const { type, } = params;
      const currentDate = new Date();
      const data = await this.noticeEntityRepository.find({
        where: {
          expirationStart: LessThanOrEqual(currentDate),
          expirationEnd: MoreThanOrEqual(currentDate),
          status: Status.ENABLED,
          type
        },
        cache: true,
        order: { createDate: 'DESC' },
      });
      return data
    } catch (error) {
      throwHttp(error.message);
    }
  }
}
